﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using OA.Manager;
using OA.Model;
using OA.Utility;

namespace OA.Bll.Personnel
{
    public class EmployeeTrainingMappingPresenter : BasePresenter<EmployeeTrainingMappingInfo>
    {
        private readonly EmployeeTrainingMappingManager _manager;

        public EmployeeTrainingMappingPresenter()
        {
            _manager = new EmployeeTrainingMappingManager();
        }

        /// <summary>
        /// 添加员工参加的培训信息(一个项目对应多个员工)
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public new string Add(JsonReq req)
        {
            string[] ids = req.Options["id"].Split(new char[] {';'});
            EmployeeTrainingMappingInfo employeeTrainingMappingInfo = null;
            int EmployeeId;
            int TrainId;
            bool bl=false;
            foreach (string id in ids)
            {
                int.TryParse(id, out EmployeeId);
                int.TryParse(req.Options["TrainId"], out TrainId);
                employeeTrainingMappingInfo = new EmployeeTrainingMappingInfo();
                employeeTrainingMappingInfo.EmployeeId = EmployeeId;
                employeeTrainingMappingInfo.TrainingId = TrainId;
                if (!_manager.IsEmployeeTrainingMappingInfo(" EmployeeId='" + employeeTrainingMappingInfo.EmployeeId.ToString(CultureInfo.InvariantCulture) + "' AND  TrainingId='"+employeeTrainingMappingInfo .TrainingId.ToString(CultureInfo.InvariantCulture)+"'"))
                {
                    bl = Manager.InsertModelInfo(employeeTrainingMappingInfo);
                    if (!bl)
                        break;
                }
                else
                    bl = true;
            }
            return JsonHelper.ResultToJson(bl, ExecuteType.Add);
        }
        /// <summary>
        /// 删除员工参加的培训信息(一个项目对应多个员工)
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public new string Delete(JsonReq req)
        {
             string[] ids = req.Options["id"].Split(new char[] {';'});
            bool bl=false;
            foreach (string id in ids)
            {
               bl= _manager.DeleteEmployeeTrainingMappingInfo(" EmployeeId='" + id + "' AND TrainingId='" + req.Options["TrainId"] +
                                                 "'");
                if(!bl)
                    break;
            }
            return JsonHelper.ResultToJson(bl, ExecuteType.Delete);
        }
        public new string Search(JsonReq req)
        {
            int pageIndex = Convert.ToInt32(req["page"]);
            int pageSize = Convert.ToInt32(req["rows"]);
            int total = 0;
            string sql = JsonHelper.GetQueryFilterString(req.Options,false);
            Type type = typeof(EmployeeTrainingMappingViewInfo);
            if (type.GetProperties().Count(a => a.Name.ToLower() == "Id") > 0)
                sql += " order by Id desc";
            DataTable dt = new Manager.ManagerParent<EmployeeTrainingMappingViewInfo>().GetSearchPaged(sql, pageIndex, pageSize,
                                                                                     ref total);
            return JsonHelper.OnDataGrid(dt, total);
        }

        /// <summary>
        /// 添加个人参加的培训信息(一个员工对应多个项目)
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public  string AddPersonalTrain(JsonReq req)
        {
            string[] ids = req.Options["id"].Split(new char[] { ';' });
            EmployeeTrainingMappingInfo employeeTrainingMappingInfo = null;
            int EmployeeId;
            int TrainId;
            bool bl = false;
            foreach (string id in ids)
            {
                int.TryParse(id, out TrainId);
                int.TryParse(req.Options["EmployeeId"], out EmployeeId);
                employeeTrainingMappingInfo = new EmployeeTrainingMappingInfo
                                                  {EmployeeId = EmployeeId, TrainingId = TrainId};
                if (!_manager.IsEmployeeTrainingMappingInfo(" EmployeeId='" + employeeTrainingMappingInfo.EmployeeId.ToString(CultureInfo.InvariantCulture) + "' AND  TrainingId='" + employeeTrainingMappingInfo.TrainingId.ToString(CultureInfo.InvariantCulture) + "'"))
                {
                    bl = Manager.InsertModelInfo(employeeTrainingMappingInfo);
                    if (!bl)
                        break;
                }
                else
                    bl = true;
            }
            return JsonHelper.ResultToJson(bl, ExecuteType.Add);
        }
        /// <summary>
        /// 删除个人参加的培训信息(一个员工对应多个项目)
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public new string DeletePersonalTrain(JsonReq req)
        {
            string[] ids = req.Options["id"].Split(new char[] { ';' });
            bool bl = false;
            foreach (string id in ids)
            {
                bl = _manager.DeleteEmployeeTrainingMappingInfo(" EmployeeId='" + req.Options["EmployeeId"] + "' AND TrainingId='" + id +
                                                  "'");
                if (!bl)
                    break;
            }
            return JsonHelper.ResultToJson(bl, ExecuteType.Delete);
        }
    }
}
